<template>
  <div>
    <el-group-title class="ccui-group-wrapper">
      <template slot="groupTitle">
        <el-form
          :model="basicForm"
          ref="tableList"
          label-width="160px"
          class="ccui-form"
          style="overflow: visible"
        >
          <div class="extend-btn">
            <stock-in-order
              style="display: inline-block; margin-right: 10px"
              :basic-form="basicForm"
              :businessType="businessType"
              @inventoryConfirm="codeNumberInventoryConfirm"
              :showStorageBtn="showStorageBtn"
            >
            </stock-in-order>
            <settle-details
              style="display: inline-block; margin-right: 10px"
              :showSettleBtn="showSettleBtn"
              @getSettlementList="getSettlementList"
            ></settle-details>
            <el-button plain type="single-number" @click="deleteRow()">
              删除行
            </el-button>
          </div>
          <el-table
            :data="tableData"
            style="margin-bottom: 20px; width: 100%"
            class="ccui-form-child-table"
            border
            ref="multipleTable"
            @select="handleCheckChange"
            @select-all="handleCheckAllChange"
            :summary-method="getSummaries"
            lazy
            show-summary
          >
            <el-table-column type="selection" width="55" />
            <el-table-column label="序号" type="index" width="50" fixed />
            <el-table-column
              :show-overflow-tooltip="true"
              prop="storageCode"
              label="入库单编号"
              min-width="180"
              fixed
            >
              <template slot-scope="scope">
                <span>{{ scope.row.storageCode }}</span>
              </template>
            </el-table-column>

            <el-table-column
              :show-overflow-tooltip="true"
              prop="storageName"
              label="入库单名称"
              min-width="300"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.storageName }}</span>
              </template>
            </el-table-column>

            <el-table-column
              :show-overflow-tooltip="true"
              prop="materialsCode"
              label="设备物资编码"
              min-width="110"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.materialsCode }}</span>
              </template>
            </el-table-column>

            <el-table-column
              :show-overflow-tooltip="true"
              prop="materialsName"
              label="设备物资名称"
              min-width="300"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.materialsName }}</span>
              </template>
            </el-table-column>

            <el-table-column
              :show-overflow-tooltip="true"
              prop="materialsDescribe"
              label="设备物资说明"
              width="200"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.materialsDescribe }}</span>
              </template>
            </el-table-column>

            <el-table-column
              :show-overflow-tooltip="true"
              prop="currentMeasuringUnit"
              label="计量单位"
              width="120"
            >
            </el-table-column>

            <el-table-column
              :show-overflow-tooltip="true"
              prop="currentNum"
              label="数量"
              min-width="150"
              header-align="left"
              :formatter="currentNum"
            >
              <template slot-scope="scope">
                <el-input
                  :placeholder="
                    scope.row.isServices == 1 || scope.row.isOtherCost == 1
                      ? ''
                      : '请输入' ||
                        (!scope.row.currentNum &&
                          !scope.row.currentMeasuringUnit)
                      ? ''
                      : '请输入'
                  "
                  v-model="scope.row.currentNum"
                  :disabled="
                    scope.row.isServices == 1 || scope.row.isOtherCost == 1
                      ? true
                      : false ||
                        (!scope.row.currentNum &&
                          !scope.row.currentMeasuringUnit)
                      ? true
                      : false
                  "
                  @input="inputNum(scope.row)"
                  @blur="inputcurrentNum($event, scope.row)"
                  maxlength="16"
                />
              </template>
            </el-table-column>
            <!-- <el-table-column
              :show-overflow-tooltip="true"
              prop="invoiceCount"
              label="数量"
              min-width="150"
              header-align="left"
              :formatter="invoiceCount"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.invoiceCount"
                  clearable
                  :disabled="((scope.row.isServices)==1 || (scope.row.isOtherCost)==1)?true:false || (!scope.row.currentNum && !scope.row.currentMeasuringUnit)?true:false"
                  @input="inputNum(scope.row)"
                  @blur="inputcurrentNum($event, scope.row)"
                />
              </template>
            </el-table-column> -->

            <el-table-column
              :show-overflow-tooltip="true"
              prop="unitPriceWithoutTax"
              label="不含税单价"
              width="150"
              header-align="left"
              align="right"
              :formatter="unitPriceWithoutTax"
            >
            </el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              prop="price"
              label="含税单价"
              width="150"
              header-align="left"
              align="right"
              :formatter="price"
            >
            </el-table-column>
            <el-table-column
              :show-overflow-tooltip="true"
              prop="taxRate"
              label="税率"
              min-width="100"
              align="right"
              header-align="left"
            >
              <template slot-scope="{ row }">
                <span>{{ row.taxRate | filterRate(that) }}</span>
              </template>
            </el-table-column>

            <el-table-column
              :show-overflow-tooltip="true"
              prop="amountExcludeTax"
              label="不含税金额"
              width="150"
              header-align="left"
              align="right"
              :formatter="amountExcludeTax"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.amountExcludeTax"
                  placeholder="请输入"
                  :disabled="
                    scope.row.isServices == 1
                      ? true
                      : false && scope.row.isOtherCost == 0
                      ? true
                      : false
                  "
                  @blur="inputAmount($event, scope.row)"
                  @input="inputNumA(scope.row)"
                  maxlength="15"
                />
              </template>
            </el-table-column>

            <el-table-column
              :show-overflow-tooltip="true"
              prop="tax"
              label="税额"
              min-width="150"
              header-align="left"
              align="right"
              :formatter="tax"
            >
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.tax"
                  placeholder="请输入"
                  :disabled="
                    scope.row.isServices == 1
                      ? true
                      : false && scope.row.isOtherCost == 0
                      ? true
                      : false
                  "
                  @blur="inputTax($event, scope.row)"
                  @input="inputNumT(scope.row)"
                  maxlength="15"
                />
              </template>
            </el-table-column>

            <el-table-column
              :show-overflow-tooltip="true"
              prop="amount"
              label="含税金额"
              min-width="150"
              align="right"
              header-align="left"
              :formatter="amount"
            >
            </el-table-column>

            <el-table-column
              :show-overflow-tooltip="true"
              prop="remark"
              label="备注"
              min-width="150"
            >
            </el-table-column>
          </el-table>
        </el-form>
      </template>
    </el-group-title>
  </div>
</template>

<script>
import StockInOrder from "./stockInOrder";
import SettleDetails from "./chooseSettlementDetails.vue";
import { formatNumber } from "@/basa/utils";
import BigNumber from "bignumber.js";
import { getFinStorageListPage } from "../../../inventoryManagement/financeWarehousing/api/edit-item";
import {mapActions} from 'vuex';
export default {
  name: "invoiceTableBill",
  components: { StockInOrder, SettleDetails },
  props: {
    basicForm: Object,
    // tableData: Array
    tableBill: Array,
    isAdd: Boolean
  },
  data() {
    return {
      reportInOrgId: 1,
      form: {},
      tableData: [],
      totalNum: "",
      laterNum: "",
      selectVal: [],
      tableNew: [],
      amountExcludeTax(row) {
        return Number(row.amountExcludeTax).toFixed(2);
      },
      price(row) {
        return Number(row.price).toFixed(2);
      },
      currentNum(row) {
        return formatNumber(row.currentNum, 3);
      },
      amount(row) {
        return Number(row.amount).toFixed(2);
      },
      tax(row) {
        return Number(row.tax).toFixed(2);
      },
      unitPriceWithoutTax(row) {
        return Number(row.unitPriceWithoutTax).toFixed(2);
      },
      that: this,
      settlCode: "",
      businessType: "",
      showStorageBtn: false, //入库单
      showSettleBtn: false //结算单
      // selectable(row){ 需求变更-贴息追涨追跌变为可以勾选
      //   console.log("row123>>>",row)
      //     if(row.isOtherCost){
      //         if(row.isOtherCost == 1){
      //             return false;
      //         } else if(row.isOtherCost == 0) {
      //             return true;
      //         }
      //     } else {
      //         if(row.currentMeasuringUnit){
      //             return true;
      //         } else {
      //             return false;
      //         }
      //     }
      // }
    };
  },
  watch: {
    tableBill: {
      handler(n) {
        this.tableData = n;
        console.log('this.tableData', this.tableData)
        this.tableNew = this.tableData;
        this.tableNew.forEach(e => {
          e.nTax = e.tax;
          e.nAmountExcludeTax = e.amountExcludeTax;
          e.nCurrentNum = e.currentNum;
          e.taxRate = e.rate;
        });
        // let $totalamount = 0; //不含税金额
        // $totalamount = this.tableData.reduce((total, currentValue) => {
        //   console.log(total, currentValue);
        //   return Number(currentValue.amountExcludeTax)
        //     ? Number(Number(total) + Number(currentValue.amountExcludeTax))
        //     : Number(total);
        // }, 0);
        // console.log("$totalamount", $totalamount);
        // let $totalTax = 0; //税额
        // $totalTax = this.tableData.reduce((total, currentValue) => {
        //   return Number(currentValue.tax)
        //     ? Number(Number(total) + Number(currentValue.tax))
        //     : Number(total);
        // }, 0);
        // console.log("$totalTax", $totalTax);
        // this.totalNum = Number($totalamount) + Number($totalTax);
        // console.log("第一次的", this.totalNum);
      }
    }
    // codeer:{
    //     handler(n){
    //         this.codder = n;
    //     }
    // }
  },
  methods: {
    ...mapActions(['setInvoicePurchase']),
    // 获取到结算单列表
    getSettlementList(val) {
      this.tableData = val;
      this.tableData.forEach(item => {
        item.materialsCode = item.materialCode; //设备物资编码
        item.materialsName = item.materialName; //设备物资名称
        item.materialsDescribe = item.materialState; //设备物资说明
        item.currentMeasuringUnit = item.measureUnit; //计量单位
        // item.unitPriceWithoutTax=item.unitPriceWithoutTax; //不含税单价
        item.price = item.unitPriceWithTax; //含税单价
        // item.rate = item.taxRate; //税率 注释掉排查税额是否正确
        item.amount = item.moneyWithTax; //含税金额
        item.remark = item.remarks; //备注
        this.$set(item, "currentNum", item.settlementCount);
        this.$set(item, "amountExcludeTax", item.moneyWithoutTax); //不含税金额
        this.$set(item, "tax", item.rate); //税额
      });
      this.tableNew = this.tableData;
      this.tableNew.forEach(e => {
        e.nTax = e.tax;
        e.nAmountExcludeTax = e.amountExcludeTax;
        e.nCurrentNum = e.currentNum;
      });
      this.setInvoicePurchase(this.tableData);
    },
    handleCheckChange(val) {
      this.selectVal = val;
    },
    handleCheckAllChange(val) {
      this.selectVal = val;
    },
    // 选择返回数据
    async codeNumberInventoryConfirm(data) {
      // console.log("data>>>>",data);
      let allID = [];
      for (let i = 0; i < data.length; i++) {
        allID[i] = data[i].id;
      }
      if (allID.length == 0) {
        return;
      } //因为运输接口没有处理，没有值就不调接口
      getFinStorageListPage({
        ids: allID,
        // businessType:this.invoicetype.businessType
        businessType: this.basicForm.invoicetype
      }).then(res => {
        if (res.data.data.rows.length > 0) {
          let arr = [];
          res.data.data.rows.forEach(value => {
            value.finStorageList.forEach(item => {
              // var settlementItem = this.basicForm.settlement.settlementList.find(
              //   _item => (_item.id = item.settlementListId)
              // );//原来的物资编码和ID从settlementItem里面取的  现在改成了都从list里面取  所以就没用这个了
              item.storageBillId = item.id; //
              item.contractBillId = item.materialId; //设备物资编码ID
              item.materialsCode = item.materialCode; //设备物资编码
              item.materialsName = item.materialName; //设备物质名称
              item.materialsDescribe = item.materialState; //设备物资说明
              item.currentMeasuringUnitCode = item.measureUnitCode; //
              item.currentMeasuringUnit = item.measureUnit; //计量单位
              // item.currentNum = item.storageCount; //数量
              item.currentNum = item.unInvoiceCount; //数量
              item.price = item.unitPriceWithTax; //含税单价
              // item.amount = item.moneyWithTax; //含税金额
              // item.amount = item.moneyWithTax; //含税金额
              // console.log('item.isServices',typeof(item.isServices))
              // console.log('item.isOtherCost',typeof(item.isOtherCost))
              if (item.isServices == 1 || item.isOtherCost == 1) {
                //两个都为0 是常规类，其他都是运费、贴息追涨之类的
                item.amountExcludeTax = item.moneyWithoutTax; //不含税金额
                item.tax = item.rate; //税额
                item.amount = item.moneyWithTax; //含税金额
                item.tax = item.tax ? item.tax.toFixed(2) : "0.00";
                item.amount = item.amount ? item.amount.toFixed(2) : "0.00";
                item.amountExcludeTax = item.amountExcludeTax
                  ? item.amountExcludeTax.toFixed(2)
                  : "0.00";
              } else {
                item.amount = new BigNumber(
                  Number(item.unInvoiceCount)
                ).multipliedBy(Number(item.unitPriceWithTax)); //含税金额(未四舍五入)

                // item.amountExcludeTax =  new BigNumber(Number(item.unInvoiceCount))
                // .multipliedBy(Number(item.unitPriceWithoutTax)).toFixed(2)//不含税金额=不含税单价*数量

                item.amountExcludeTax = new BigNumber(
                  item.amount / (1 + item.taxRate * 1)
                ).toFixed(2); //不含税金额=含税金额/(1+税率)

                item.amount = new BigNumber(item.amount).toFixed(2); //含税金额(不含税金额计算完后四舍五入)
                item.tax = new BigNumber(Number(item.amount))
                  .minus(Number(item.amountExcludeTax))
                  .toFixed(2); //税额=含税金额-不含税金额
              }
              item.rate = item.taxRate; //税率
              item.unitPriceWithoutTax = new BigNumber(
                Number(item.unitPriceWithTax) / (1 + item.taxRate * 1)
              ).toFixed(2); //不含税单价=含税单价/(1+税率)
              // item.remark = value.finStorage.remark?value.finStorage.remark:""; //备注
              item.remark = item.remark ? item.remark : ""; //备注
              item.taxFirst = item.tax; //税额初始值
              item.amountExcludeTaxFirst = item.amountExcludeTax; //不含税金额初始值

              arr.push(item);
              this.tableData = arr;
              this.tableData.forEach(e => {
                if (e.currentNum) {
                  e.currentNum = formatNumber(e.currentNum, 3);
                }

                e.storageBillRate = e.tax; //税额
                e.storageBillMoneyWithoutTax = e.amountExcludeTax; //不含税金额
                // e.unitPriceWithoutTax = Number(e.unitPriceWithTax).toFixed(2);
                // e.price = Number(e.price).toFixed(2);
                // e.amountExcludeTax = Number(e.amountExcludeTax).toFixed(2);
                // e.tax = Number(e.tax).toFixed(2);
                // e.amount = Number(e.amount).toFixed(2)
              });
              this.setInvoicePurchase(this.tableData);
              this.tableNew = this.tableData;
              this.tableNew.forEach(e => {
                e.nTax = e.tax;
                e.nAmountExcludeTax = e.amountExcludeTax;
                e.nCurrentNum = e.currentNum;
              });
              let $totalamount = 0; //不含税金额
              $totalamount = this.tableData.reduce((total, currentValue) => {
                return Number(currentValue.amountExcludeTax)
                  ? Number(total + currentValue.amountExcludeTax)
                  : total;
              }, 0);
              // console.log('$totalamount',$totalamount)
              let $totalTax = 0; //税额
              $totalTax = this.tableData.reduce((total, currentValue) => {
                return Number(currentValue.tax)
                  ? Number(total + currentValue.tax)
                  : total;
              }, 0);
              // console.log('$totalTax',$totalTax)
              this.totalNum = $totalamount + $totalTax;
              // console.log("第一次的", this.totalNum);
            });
          });
        }
        // if (res.data.data.finStorageList.length > 0) {
        //   let arr = [];

        // }
      });
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (
          ["currentNum", "amount", "amountExcludeTax", "tax"].includes(
            column.property
          )
        ) {
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                let num = Number(prev * 100) + Number(curr * 100);
                return num / 100;
              } else {
                return Number(prev);
              }
            }, 0);
            if (index == 12 || index == 13 || index == 14) {
              // sums[index] = Number(sums[index]).toFixed(2);
              sums[index] = new BigNumber(sums[index]).toFixed(2);
              //   console.log(sums[index])
            }
            if (index == 8) {
              // sums[index] = new BigNumber(Number(sums[index])).toFormat(3)
              sums[index] = new BigNumber(sums[index]).toFixed(3);
            }
          }
        }
      });
      this.laterNum = Number(sums[12]) + Number(sums[13]);
      // console.log("第一次", this.totalNum, "修改后的", this.laterNum);
      this.$bus.$emit("invoiceTableBill", [sums[12], sums[13], sums[14]]);
      return sums;
    },
    deleteRow() {
      console.log(this.selectVal);
      if (this.selectVal.length <= 0) {
        // this.$message({
        //   showClose: true,
        //   message: '5',
        //   type: "error",
        //   ccuiCustom: true
        // });
        this.$showErrorMessage("请至少选择一项");
        return;
      }
      var that = this;
      // 拿到选中的数据；
      var val = this.selectVal;
      // 如果选中数据存在
      if (val) {
        // 将选中数据遍历
        val.forEach(function(item, index) {
          // 遍历源数据
          that.tableData.forEach(function(itemI, indexI) {
            // 如果选中数据跟元数据某一条标识相等，删除对应的源数据
            if (item.id === itemI.id) {
              that.tableData.splice(indexI, 1);
            }
          });
        });
      }
      // 清除选中状态
      this.$refs.multipleTable.clearSelection();
      this.setInvoicePurchase(this.tableData);
    },
    // 数量失焦
    inputcurrentNum(e, row) {
      // console.log("row>>>",row)
      // console.log("this.tableData>>>",this.tableNew);
      // console.log("isAdd>>>",this.isAdd)
      // 控制输入为空时数量值为0不为空
      if (
        row.currentNum == "" ||
        row.currentNum == null ||
        row.currentNum == 0
      ) {
        this.tableNew.forEach((ele, i) => {
          if (ele.id == row.id) {
            e.target.value = Number(ele.nCurrentNum);
          }
        });
      }

      if (this.isAdd) {
        this.tableNew.forEach((ele, i) => {
          if (ele.id == row.id && Number(ele.nCurrentNum) < row.currentNum) {
            e.target.value = Number(ele.nCurrentNum);
            this.$message({
              type: "error",
              message: "输入数量不能大于原始数量",
              showClose: true,
              ccuiCustom: true,
              duration: 8000
            });
          }
        });
      } else {
        this.tableNew.forEach((ele, i) => {
          if (
            ele.id == row.id &&
            Number(ele.nCurrentNum) + ele.unInvoiceCount < row.currentNum
          ) {
            e.target.value = Number(ele.nCurrentNum);
            this.$message({
              type: "error",
              message: "输入数量不能大于原始数量",
              showClose: true,
              ccuiCustom: true,
              duration: 8000
            });
          }
        });
      }

      // console.log("this.tableNew>>>",this.tableNew);
      this.tableNew.forEach((ele, i) => {
        if (ele.id == row.id) {
          if (isNaN(e.target.value * 1)) {
            row.currentNum = ele.nCurrentNum;
            let amount = Number(row.currentNum) * Number(row.price); //数量*含税单价=含税金额
            // let amountExcludeTax = Number(row.unitPriceWithoutTax) * Number(row.currentNum);//数量*不含税单价=不含税金额
            let amountExcludeTax = new BigNumber(amount / (1 + row.taxRate * 1)); //含税金额/（1+税率）=不含税金额
            let tax = Number(amount) - Number(amountExcludeTax); //税额=含税金额-不含税金额
            this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
            this.$set(
              row,
              "amountExcludeTax",
              Number(amountExcludeTax).toFixed(2)
            );
            this.$set(row, "tax", Number(tax).toFixed(2));
          } else {
            if (e.target.value * 1 < 0) {
              row.currentNum = ele.nCurrentNum;
              let amount = Number(row.currentNum) * Number(row.price); //数量*含税单价=含税金额
              // let amountExcludeTax=Number(row.unitPriceWithoutTax)*Number(row.currentNum);//数量*不含税单价=不含税金额
              let amountExcludeTax = new BigNumber(amount / (1 + row.taxRate * 1)); //含税金额/（1+税率）=不含税金额
              let tax = Number(amount) - Number(amountExcludeTax); //税额=含税金额-不含税金额
              this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
              this.$set(
                row,
                "amountExcludeTax",
                Number(amountExcludeTax).toFixed(2)
              );
              this.$set(row, "tax", Number(tax).toFixed(2));
            } else {
              row.currentNum = formatNumber(e.target.value, 3);
              let amount = Number(row.currentNum) * Number(row.price); //数量*含税单价=含税金额
              // let amountExcludeTax=Number(row.unitPriceWithoutTax)*Number(row.currentNum);//数量*不含税单价=不含税金额
              let amountExcludeTax = new BigNumber(amount / (1 + row.taxRate * 1)); //含税金额/（1+税率）=不含税金额
              let tax = Number(amount) - Number(amountExcludeTax); //税额=含税金额-不含税金额
              this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
              this.$set(
                row,
                "amountExcludeTax",
                Number(amountExcludeTax).toFixed(2)
              );
              this.$set(row, "tax", Number(tax).toFixed(2));
            }
          }
        }
      });
      this.setInvoicePurchase(this.tableData);
    },
    // 数量input
    inputNum(row) {
      //   let amount = row.currentNum * row.price;
      //   this.$set(row, "amount", formatNumber(amount, 2)); //含税金额=数量*不含税单价
      // isNaN返回true不是一个数字
      if (isNaN(row.invoiceCount)) {
        return;
      }
      if (isNaN(row.amountExcludeTax)) {
        let amount = Number(row.currentNum) * Number(row.price); //数量*含税单价=含税金额
        // let amountExcludeTax=Number(row.unitPriceWithoutTax) * Number(row.currentNum);//数量*不含税单价=不含税金额
        let amountExcludeTax = new BigNumber(amount / (1 + row.taxRate * 1)); //含税金额/（1+税率）=不含税金额
        let tax = Number(amount) - Number(amountExcludeTax); //税额=含税金额-不含税金额
        this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
        this.$set(row, "amountExcludeTax", Number(amountExcludeTax).toFixed(2));
        this.$set(row, "tax", Number(tax).toFixed(2));
      } else {
        // console.log("我不懂走");
        let amount = Number(row.currentNum) * Number(row.price); //数量*含税单价=含税金额
        // let amountExcludeTax=Number(row.unitPriceWithoutTax)*Number(row.currentNum);//数量*不含税单价=不含税金额
        let amountExcludeTax = new BigNumber(amount / (1 + row.taxRate * 1)); //含税金额/（1+税率）=不含税金额
        let tax = Number(amount) - Number(amountExcludeTax); //税额=含税金额-不含税金额
        this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
        this.$set(row, "amountExcludeTax", Number(amountExcludeTax).toFixed(2));
        this.$set(row, "tax", Number(tax).toFixed(2));
      }
      this.setInvoicePurchase(this.tableData);
    },

    // 不含税金额 失焦
    inputAmount(e, row) {
      this.tableNew.forEach((ele, i) => {
        if (ele.id == row.id) {
          if (isNaN(e.target.value * 1)) {
            row.amountExcludeTax = ele.nAmountExcludeTax;
            row.amountExcludeTax = new BigNumber(
              Number(row.amountExcludeTax)
            ).toFixed(2);
            let amount = Number(row.tax) + Number(row.amountExcludeTax);
            this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
          } else {
            if (e.target.value * 1 < 0 && ele.isOtherCost !== 1) {
              row.amountExcludeTax = ele.nAmountExcludeTax;
              row.amountExcludeTax = new BigNumber(
                Number(row.amountExcludeTax)
              ).toFixed(2);
              let amount = Number(row.tax) + Number(row.amountExcludeTax);
              this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
            } else {
              // costStatus 1、贴息 2、价格追涨 3、价格追跌 4、其他
              // if(row.costStatus==2 && e.target.value<0){
              //   e.target.value =e.target.value*(-1)
              // };
              // if(row.costStatus==3 && e.target.value>0){
              //   e.target.value =e.target.value*(-1)
              // }
              row.amountExcludeTax = Number(e.target.value);
              row.amountExcludeTax = new BigNumber(
                Number(row.amountExcludeTax)
              ).toFixed(2);
              let amount = Number(row.tax) + Number(row.amountExcludeTax);
              this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
            }
          }
        }
        // console.log(row.amount)
      });
      //不含税金额和税额合计变动值不能超过100
      // if (Math.abs(this.totalNum - this.laterNum) > 100) {
      //   console.log("222", Math.abs(this.totalNum - this.laterNum));
      //   this.$message({
      //       type:'error',
      //       message:"不含税金额和税额合计变动值不能超过100",
      //       showClose:true,
      //       ccuiCustom:true,
      //       duration:8000
      //   });
      //   this.$emit("fixMoney", 1);
      // } else {
      //   this.$emit("fixMoney", 0);
      // }
      this.setInvoicePurchase(this.tableData);
    },
    // 不含税金额 input
    inputNumA(row) {
      // isNaN返回true不是一个数字
      if (isNaN(row.amountExcludeTax)) {
        // console.log(row.nAmountExcludeTax, row.nTax);
        let amount = Number(row.nTax) + Number(row.nAmountExcludeTax);
        //   console.log(row.tax, row.amountExcludeTax, "amount", amount);
        this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
      } else {
        let amount = Number(row.tax) + Number(row.amountExcludeTax);
        this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
      }
      this.setInvoicePurchase(this.tableData);
    },
    // 税额输入 失焦
    inputTax(e, row) {
      // console.log("-------");
      this.tableNew.forEach(ele => {
        if (ele.id == row.id) {
          // console.log(ele);
          // if(isNaN(e.target.value)){return}
          if (isNaN(e.target.value * 1)) {
            row.tax = ele.nTax;
            row.tax = new BigNumber(Number(row.tax)).toFixed(2);
            // row.tax=Number(row.tax).toFixed(2)
            let amount = Number(row.tax) + Number(row.amountExcludeTax);
            this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
          } else {
            if (e.target.value * 1 < 0 && ele.isOtherCost !== 1) {
              row.tax = ele.nTax;
              row.tax = new BigNumber(Number(row.tax)).toFixed(2);
              // row.tax=Number(row.tax).toFixed(2)
              let amount = Number(row.tax) + Number(row.amountExcludeTax);
              this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
            } else {
              // costStatus 1、贴息 2、价格追涨 3、价格追跌 4、其他
              // if(row.costStatus==2 && e.target.value<0){
              //   e.target.value =e.target.value*(-1)
              // };
              // if(row.costStatus==3 && e.target.value>0){
              //   e.target.value =e.target.value*(-1)
              // }
              row.tax = Number(e.target.value);
              row.tax = new BigNumber(Number(row.tax)).toFixed(2);
              // row.tax=Number(row.tax).toFixed(2)
              let amount = Number(row.tax) + Number(row.amountExcludeTax);
              this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
            }
          }
        }
      });
      // if (Math.abs(this.totalNum - this.laterNum) > 100) {
      //   this.$message({
      //       type:'error',
      //       message:"不含税金额和税额合计变动值不能超过100",
      //       showClose:true,
      //       ccuiCustom:true,
      //       duration:8000
      //   });
      //   this.$emit("fixMoney", 1);
      // } else {
      //   this.$emit("fixMoney", 0);
      // }
      this.setInvoicePurchase(this.tableData);
    },
    // 税额计算 input
    inputNumT(row) {
      if (isNaN(row.tax)) {
        let amount = Number(row.nTax) + Number(row.nAmountExcludeTax);
        this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
      } else {
        // console.log("我不懂走");
        let amount = Number(row.tax) + Number(row.amountExcludeTax);
        this.$set(row, "amount", Number(amount).toFixed(2)); //含税金额=不含税金额+税额
      }
      this.setInvoicePurchase(this.tableData);
    }
  },
  filters: {
    filterRate(val, that) {
      return new that.$BigNumber(Number(val)).multipliedBy(100) + "%";
    }
  },
  created() {
    this.$bus.$on("businessType", message => {
      this.basicForm.invoicetype = message;
      // console.log(this.basicForm,this.basicForm['invoicetype'])
      if (
        this.basicForm["invoicetype"] == "10" ||
        this.basicForm["invoicetype"] == "20"
      ) {
        this.showStorageBtn = false;
        this.showSettleBtn = true;
      } else if (this.basicForm["invoicetype"] == "30") {
        this.showStorageBtn = true;
        this.showSettleBtn = false;
      }
    });
  }
};
</script>

<style scoped lang="scss">
/deep/
  .ccui-form-child-table
  .el-table
  .el-table__footer-wrapper
  tbody
  td:nth-child(1) {
  color: #4a4a4a;
}
/deep/ .el-table__footer-wrapper tbody td {
  font-size: 13px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 600;
  color: #0089ef;
}
/deep/ .el-table__fixed-header-wrapper {
  th {
    pointer-events: auto;
  }
}

/deep/ .el-table__fixed,
/deep/.el-table__fixed-right {
  pointer-events: none;
  td {
    pointer-events: auto;
  }
}

::v-deep {
  .el-input input {
    padding-right: 50px;
  }
}
</style>
